import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';

import '../../foundation/jd_colors.dart';
import '../../router/router_util.dart';
import '../../utils/loading_util.dart';

class PreviewImagePage extends StatelessWidget {
  final PageController pageController;
  final List<ImageProvider> imageProvider;
  final ValueNotifier<String> _currentIndexValueNotifier;

  PreviewImagePage({
    super.key,
    int index = 0,
    required this.imageProvider,
  })  : _currentIndexValueNotifier =
            ValueNotifier('${index + 1}/${imageProvider.length}'),
        pageController = PageController(initialPage: index);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: JDColors.black,
      body: Stack(children: <Widget>[
        PhotoViewGallery.builder(
          pageController: pageController,
          itemCount: imageProvider.length,
          loadingBuilder: (BuildContext context, ImageChunkEvent? event) =>
              kLoadingView,
          onPageChanged: (index) => _currentIndexValueNotifier.value =
              '${index + 1}/${imageProvider.length}',
          builder: (BuildContext context, int index) =>
              PhotoViewGalleryPageOptions(
            imageProvider: imageProvider[index],
            onTapUp: (context, details, controllerValue) {
              RouterUtil.pop(context);
            },
          ),
        ),
        AppBar(
          title: ValueListenableBuilder(
            valueListenable: _currentIndexValueNotifier,
            builder: (BuildContext context, String value, Widget? child) {
              return Text(
                value,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: Theme.of(context)
                    .textTheme
                    .titleLarge!
                    .copyWith(fontSize: 18)
                    .copyWith(color: Colors.black),
              );
            },
          ),
          backgroundColor: JDColors.transparent,
        ),
      ]),
    );
  }
}
